package com.xzx.dynamicpermission;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();

    // 这个页面所需的全部权限
    private static final String[] PERMISSIONS =
            new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final int PERMISSION_REQUEST_CODE = 0; // 系统权限管理页面的参数
    //权限检测器
    private PermissionsChecker mPermissionsChecker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPermissionsChecker = new PermissionsChecker(this);

        //        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
        //        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        //            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        //        } else {
        //            TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        //            String uid = telephonyMgr.getDeviceId();
        //            Log.i("tag", uid);
        //        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (Build.VERSION.SDK_INT >= 23) {
            for (String permission : PERMISSIONS) {
                if (mPermissionsChecker.lacksPermission(permission)) {
                    mPermissionsChecker.requestPermissions(this, new String[]{permission}, PERMISSION_REQUEST_CODE);
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Toast.makeText(this, "==" + grantResults[0], Toast.LENGTH_LONG).show();
        if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "=====所有的权限已授权====");
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Log.i(TAG, "=====有权限没有授权，需要重新授权");
                mPermissionsChecker.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST_CODE);
            } else {
                Log.i(TAG, "=====不在询问，并拒绝===");
                mPermissionsChecker.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST_CODE);
            }
        }
    }

    public void test(View view) {
        Toast.makeText(this, "测试", Toast.LENGTH_LONG).show();
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "没有权限，请求授权");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
            return;
        } else {
            TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            String imei = telephonyMgr.getDeviceId();
            Toast.makeText(this, "imei:" + imei, Toast.LENGTH_LONG).show();
            Log.i(TAG, "===============" + imei);
        }
    }
}
